//
//  SellerHomeCtr-header.swift
//  smosky
//
//  Created by zhu xietong on 2018/5/21.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import soapp
import Eelay

extension SellerHomeCtr{
    
    class Header: JoView {
        let headV = UIImageView()
        let nameL = UILabel()
        let infoL = UILabel()
        
        let careBt = UIButton()
        let careL = UILabel()
        override func addLayoutRules() {
            let careBk = UIView()
            eelay = [
                [headV,[ee.L.B,[18,-10]],"50",50],
                [nameL,[headV,ee.R.Y,ee.L.Y,[10,-8]],[ee.R,-90]],
                [infoL,[headV,ee.R.Y,ee.L.Y,[10,11]],[ee.R,-90]],
                [careBk,[ee.B.R],"59",59],
            ]
            let shop = "[XX]旗舰店".replaceVars(values: Goods.self_operated_tag)
            nameL.ui.font(font: UIFont.systemFont(ofSize: 12, weight: .black)).cl_fff.text(shop)
            infoL.ui.font11.color(hex: "#E46D1E").text("\("综合评分".loc)：4.8")
            headV.ui.holder(type: .grayBack).set(radius: 25, width: 1, color: UIColor(shex: "#fff4"))
            headV.img_url = __heads.one_str
            
            careBk.eelay = [
                [careBt,[ee.X.Y,[0,-10]]],
                [careL,[ee.X.Y,[0,10]]],
            ]
            careBk.bsui.background(hex: "#DB340D")
            careBt.ui.nimage(UIImage(named: "dp_xq_gzl"))
            careL.ui.center.font11.text(hex: "#fff").text("关注".loc)
        }
        
        override func layoutSubviews() {
            super.layoutSubviews()
            
            if self.subviews.filter({ (one) -> Bool in
                if one is GradientView{return true}
                return false
            }).count > 0
            {
                return
            }
            let v = GradientView(frame: [0,0,self.frame.size.width,80], red: 0.0, green: 0.0, blue: 0.0)
            v.alpha = 1
            insertSubview(v, belowSubview: headV)
        }
        override var intrinsicContentSize: CGSize{
            return [Swidth,80]
        }
    }
    class SellerHeader: JoView {
        
        public var justHeight:CGFloat{
            get{
                let just = self.mo.controller?.topLayoutGuide.length ?? TP.topHeight
                return just
            }
        }
        public var miniHeigth:CGFloat{
            get{
                return self.frame.size.height + justHeight
            }
        }
        
        var heightLayoutConstraint = NSLayoutConstraint()
        
        let containerView = UIView()
        
        let imageV = UIImageView()
     
        
        
        
        let seller = Header()
        
        override func addLayoutRules() {
            self.backgroundColor = UIColor.white
            
            self.eelay = [
                [containerView,[ee.L.R.B]],
            ]
            
            self.heightLayoutConstraint = NSLayoutConstraint(item: containerView, attribute:.height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: self.miniHeigth)
            self.addConstraint(heightLayoutConstraint)
            containerView.eelay = [
                [imageV,[ee.T.L.B.R,[-1,-1,1,1]]],
                [seller,[ee.L.B.R]],
            ]
            
            imageV.img_url = __images.one_str
        }
       
        
      
    
        func scrollViewDidScroll(scrollView: UIScrollView) {
            let offsetY = (-scrollView.contentOffset.y) - TP.topHeight
            if offsetY < 0{
                return
            }
            self.heightLayoutConstraint.constant = miniHeigth+offsetY
            self.containerView.layoutIfNeeded()
            containerView.clipsToBounds = offsetY <= 0
        }
    }
}
